package com.yzx.lifeassistants.model.analysis;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yzx.lifeassistants.bean.Book;
import com.yzx.lifeassistants.utils.LogcatUtils;

/**
 * @Description: 解析图书检索返回的结果
 * @author: yzx
 * @time: 2016-3-8 上午10:26:44
 */
public class QueryBookAnalysis {

	/**
	 * 
	 * @Description: 结果是否为空
	 */
	public static Boolean isNull(String response) {
		Boolean isNull = true;
		try {
			JSONObject jsonObject = new JSONObject(response);
			if (jsonObject.getBoolean("status")) {
				isNull = false;
			} else {
				String errMsg = jsonObject.getString("errMsg");
				LogcatUtils.e("解析图书检索返回的结果失败", errMsg);
			}
		} catch (JSONException e) {
			if (null != e.getMessage()) {
				LogcatUtils.e("解析图书检索返回的结果异常", e.getMessage());
			}
		}
		return isNull;
	}

	/**
	 * 
	 * @Description: 解析返回的图书列表
	 */
	public static List<Book> getData(String response) {
		List<Book> books = new ArrayList<Book>();
		try {
			JSONObject jsonObject = new JSONObject(response);
			JSONArray array = jsonObject.getJSONArray("data");
			for (int i = 0; i < array.length(); i++) {
				JSONObject object = array.getJSONObject(i);
				Book book = new Book();
				book.setName(object.getString("name"));
				book.setAuthor(object.getString("author"));
				book.setPublisher(object.getString("publisher"));
				book.setYear(object.getString("year") + "年");
				if (null == object.getString("place")
						|| "".equals(object.getString("place"))) {
					book.setPlace("编目中");
				} else {
					book.setPlace(object.getString("place"));
				}
				book.setAmount(object.getString("amount"));
				book.setAppointmentId(object.getString("appointment_id"));
				book.setUrl(object.getString("url"));
				books.add(book);
			}
		} catch (JSONException e) {
			if (null != e.getMessage()) {
				LogcatUtils.e("解析图书检索返回的结果异常", e.getMessage());
			}
		}
		return books;

	}
}
